<?php defined('SYSPATH') OR die('No direct access allowed.');
/**
 * Default Kohana controller. This controller should NOT be used in production.
 * It is for demonstration purposes only!
 *
 * @package    Core
 * @author     Kohana Team
 * @copyright  (c) 2007-2008 Kohana Team
 * @license    http://kohanaphp.com/license.html
 */
class BaseController extends Template_Controller {

	// Disable this controller when Kohana is set to production mode.
	// See http://docs.kohanaphp.com/installation/deployment for more details.
	const ALLOW_PRODUCTION = FALSE;

	// Set the name of the template to use
	public $template = 'kohana/Estudy_Template';
	public $requestUrl = 'requested_url';
	public $breadcrumb = '';
	public $limit = 0;
	public $pagination ;
	public $session;
	public $user ;
	public function __construct()
	{
		parent::__construct();
		//$config = Kohana::config('pagination');
		$this->limit = (int) Kohana::config('pagination.default.items_per_page');
		$this->template->breadcrumb = $this->get_breadcrumbs();
		// Display the template immediately after the controller method
		Event::add('system.post_controller', array($this, '_display'));
		//Event::add('system.post_controller', array($this, 'CheckLogin'));
		$this->CheckLogin();
	}
	public function CheckLogin()
	{
		$this->session= Session::instance();
		if (!Auth::instance()->logged_in()){
			$this->session->set($this->requestUrl,"/".url::current()); // this will redirect from the login page back to this page/
			url::redirect('user/login');  //echo 'this is check login method';

		}else{
			$this->user = Auth::instance()->get_user();
			$this->template->username = $this->user->username;
			//$this->user = $this->authentic->get_user(); //now you have access to user information stored in the database
			//$this->user->add(ORM::factory('role','login'));
		}
	}
	public function get_breadcrumbs()
	{
		global $breadcrumbs;

		$get_breadcrumbs = html::breadcrumb();
		while(current($get_breadcrumbs))
		{
			// Check if we have reached the last crumb
			if(key($get_breadcrumbs) < (count($get_breadcrumbs)-1))
			{
				// If we haven't, add a breadcrumb separator
				$breadcrumbs .= current($get_breadcrumbs).' / ';
			}
			else
			{
				// If we have, remove the anchor from the breadcrumb and make it bold
				$breadcrumbs .= strip_tags("<strong>".current($get_breadcrumbs)."</strong>", "<strong>");
			}
			next($get_breadcrumbs);
		}
		return $breadcrumbs;
	}
	public function __call($method, $arguments)
	{
		// Disable auto-rendering
		$this->auto_render = FALSE;

		// By defining a __call method, all pages routed to this controller
		// that result in 404 errors will be handled by this method, instead of
		// being displayed as "Page Not Found" errors.
		echo 'This text is generated by __call. If you expected the index page, you need to use: welcome/index/'.substr(Router::$current_uri, 8);
	}

}
